/*
*******************************************************************************
* ansi c source code
* file name:
*	GenerateData.c
* abstract:
*	to generate data needed be transmitted and generate PN for the trainning symbol
* author:Fangming He
*******************************************************************************
*/



/*
*******************************************************************************
*                               include files
*******************************************************************************
*/
#include <stdio.h>
#include <math.h>
#include <time.h>
#include "globaldef.h"
#include "Random.h"
#include "Modulator_PN.h"
#include "bit_rev.h"
#include "globalVai.h"




/*
*******************************************************************************
*                            function definition
*******************************************************************************
*/


/*
*******************************************************************************
* description:
*	to make a packet data to be transmitted 
*   A packet data consists of 10 OFDM symbols  
*  input :
*  M_Type : the modulator type while determine the data size corresponding to a 
            fixed number modulated symbol
*  output :
*  Source :  the arrry for storing the data
* author:
*	guo liang       2006-05-30
* revision history:
*	guo liang       2006-05-30	original version
*******************************************************************************
*/
int GenerateData(int M_Type , int *source)
{
	time_t t; 
	int i;
	int total;
	int coeff;       /* the ratio of the modulated symbol to the bits */
	coeff =(int)(2 * pow(2,M_Type));
	t = time(0);
	SetRand2( t  );
	total = DATANUM*coeff*SYMBNUM;
	for ( i = 0 ; i < total ; i++)
	{
		if( RunRand2() < 0.5 )
			source[i] = 0;
			else source[i] = 1;
	}
	return 1;
}

/*
*******************************************************************************
*                            function definition
*******************************************************************************
*/
/*
*******************************************************************************
* description:
*	to make the PN sequence for the training symbol
*  input
*  output
*  PN1  : to store the first training Symbol
*  PN2  : to store the second training Symbol
*  PN_Diff : to store the difference PN between the first and second training symbol on the even sub_carriers 
*  referenced  :
*  Modulator_PN :  
* author:
*	guo liang       2006-05-31
* revision history:
*	guo liang       2006-05-31	original version
*******************************************************************************
*/

int Generate_PN(struct Complex *PN1,struct Complex *PN2, struct Complex *PN_Diff)
{
	int i;
	if( Modulator1(FFT_Size,PN1) == 0)
	{
		printf("modulate pn for the first training symbol error \n");
		return 0;
	}
	if ( Modulator2(FFT_Size,PN_Diff) == 0)
	{
		printf("modulate difference pn between the two training symbol error \n");
		return 0;
	}
    for (i = 0; i < FFT_Size/2; i++)            /* to make the second training symbol according to PN_Diff*/
	{
		PN2[2*i] = Mul_complex(PN1[2*i],PN_Diff[2*i]);
	}
	for (i=0; i< FFT_Size/2; i++)              /*  the odd sub_carriers on the second training symbol retained */ 
	{
		PN2[2*i+1] = Mul_complex(PN2[2*i],PN_Diff[2*i]);
	}
	for (i=0; i<FFT_Size;i++)          /* to normallized the energy to 1*/
	{
		PN2[i].real /= sqrt(2);
		PN2[i].imag /= sqrt(2);
	}
	return 1;
}
